package com.example.fengshoutong.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.fengshoutong.MainApplication;
import com.example.fengshoutong.R;
import com.example.fengshoutong.bean.User;
import com.example.fengshoutong.greendao.UserDao;
import com.example.fengshoutong.utils.Constant;
import com.example.fengshoutong.utils.FragmentUtils;
import com.example.fengshoutong.utils.ToastUtils;

import java.util.List;

/**
 * 修改登录密码页面
 */
public class PasswordFragment extends Fragment {
    // 用户数据库
    private UserDao userDao;
    // 旧密码编辑栏
    private EditText edit_old_password;
    // 新密码编辑栏
    private EditText edit_new_password;
    // 再次输入新密码的编辑栏
    private EditText edit_twice_password;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_password, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();
        // 获取用户数据库
        userDao = MainApplication.getInstance().getUserDaoSession().getUserDao();
        initView();
    }

    private void initView() {
        edit_old_password = getView().findViewById(R.id.edit_old_password);
        edit_new_password = getView().findViewById(R.id.edit_new_password);
        edit_twice_password = getView().findViewById(R.id.edit_twice_password);
        getView().findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String oldPassword = edit_old_password.getText().toString();
                String newPassword = edit_new_password.getText().toString();
                String twicePassword = edit_twice_password.getText().toString();
                if (oldPassword == null || newPassword == null || twicePassword == null) {
                    ToastUtils.getInstance().makeToast("请完整填写！");
                    return;
                }
                if (!newPassword.equals(twicePassword)) {
                    ToastUtils.getInstance().makeToast("两次输入的密码不一样！");
                    return;
                }
                // 在用户数据库中按当前账号搜索
                List<User> users = userDao.queryBuilder().where(UserDao.Properties.Account.eq(Constant.userAccount)).list();
                for (User user : users) {
                    // 搜索到该账号，先匹配旧密码是否正确
                    if (user.getPassword().equals(oldPassword)) {
                        // 旧密码正确，更新新密码到用户数据库中，并提示修改成功，返回设置页面
                        userDao.update(new User(Constant.userAccount, newPassword));
                        ToastUtils.getInstance().makeToast("密码修改成功！");
                        FragmentUtils.getInstance().switchFragment(R.id.nav_setting);
                        return;
                    }
                }
                ToastUtils.getInstance().makeToast("旧密码错误！");

            }
        });
    }


}
